//
//  TSPasswordConfirmViewController.swift
//  TopStock
//
//  Created by dyd on 2017/8/8.
//  Copyright © 2017年 dyd. All rights reserved.
//

import UIKit

class TSPasswordConfirmViewController: UIViewController {
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var resetPwdTextField: UITextField!
    var isFromSetting:Bool = false
    
    var resetPwdToken:String?
    
    let tsRequest = TSRequestData()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func backItemAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }

    @IBAction func checkPasswordAction(_ sender: UIButton){
        let password = passwordTextField.text
        let resetPwd = resetPwdTextField.text
        if password == resetPwd{
            let requestDic = tsRequest.resetPwd(password: password, resetPwdToken: resetPwdToken)
            let status = requestDic["success"] as! Bool
            if status{
                if isFromSetting
                {
                    alertViewController(title: "密码修改成功", message: nil)
                }else{
                     alertViewController(title: "密码修改成功", message: "返回首页重新登陆")
                }
            }else{
                let error = requestDic["error"] as! NSDictionary
                showAlertView(title: nil, message: error["errorInfo"] as? String)
            }
        }else{
            showAlertView(title: nil, message: "两次输入的密码不一致")
        }
    }
    
    func showAlertView(title:String?,message:String?)
    {
        let alertController = UIAlertController(title: title,
                                                message: message, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default, handler: {
            action in
        })
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
    
    func alertViewController(title:String?,message:String?){
        let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction.init(title: "确定", style: .`default`, handler: {
            (UIAlertAction) -> Void in
            self.dismiss(animated: true, completion: nil)
        }))
        self.present(alert, animated: true, completion: nil)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
